﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using System.Text.RegularExpressions;
using ZyGames.Framework.Net;
using ZyGames.DZPocker.Tools;
using ZyGames.Framework.Common.Serialization;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 房主申请功能房主功能
	/// </summary>
	public class Action30085 : BaseAction
	{
		int tableId;
		int userId;
		string userDataStr = "";
		int maxMultiple = 1;
		//玩家带入控制
		bool playerApplyChipControl = false;
		//牌局暂停
		bool pause = false;
		private bool result = false;
		public Action30085(HttpGet actionGetter)
			: base((short)ActionType.DZApplyRoomMasterFunction, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TableId", ref tableId) &&
				httpGet.GetInt("UserId", ref userId))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			var _currentTable = GameRoom.Current.GetTableData(tableId);
			if (_currentTable == null)
			{
				Tool.WriteWarmingLine("消息30085获得房间不存在");
				return false;
			}
			var _currentGameUser = GameTable.Current.GetUser(userId);
			if (_currentGameUser == null)
			{
				Tool.WriteWarmingLine("消息30085玩家不存在");
				return false;
			}
			if(userId != _currentTable.TableMaster.UserId)
			{
				Tool.WriteErrorLine("消息30085玩家不是房主，不好做响应的操作");
				return false;
			}
			var userData = new List<SimpleUserData>();
			foreach(var userid in _currentTable.ApplyChipWithoutConfirmUserIDList)
			{
				var u = GameTable.Current.GetUser(userid);
				if(u != null)
				{
					var tempUser = new SimpleUserData() { HeadIcon = u.HeadIcon, NickName = u.NickName, UserId = u.UserId };
					userData.Add(tempUser);
				}
			}
			this.userDataStr = Convert.ToBase64String(ProtoBufUtils.Serialize(userData));
			this.maxMultiple = _currentTable.MaxMultiple;
			this.playerApplyChipControl = _currentTable.PlayerApplyChipControlSwitch;
			this.pause = _currentTable.Pause;

			result = true;
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(this.result);
			this.PushIntoStack(this.tableId);
			this.PushIntoStack(this.maxMultiple);
			this.PushIntoStack(this.userDataStr);
			this.PushIntoStack(this.playerApplyChipControl);
			this.PushIntoStack(this.pause);
		}
	}
}
